package com.qn.example.mapper;

import com.qn.example.pojo.AddressBook;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author 21276
 * @version 1.0
 * @date 2023/5/19 9:17
 */
@Mapper
public interface AddressBookMapper {
    //添加地址
    @Insert("insert into address_book(id,user_id,consignee,phone,sex,detail,label,create_time,update_time,create_user,update_user) " +
            "values(#{id},#{userId},#{consignee},#{phone},#{sex},#{detail},#{label},#{createTime},#{updateTime},#{createUser},#{updateUser})")
    void saveAddressBook(AddressBook addressBook);

    @Select("select * from address_book where user_id=#{userId} order by update_time desc")
    List<AddressBook> addressList(Long userId);

    @Update("update address_book set is_default = 0 where user_id = #{userId}")
    void removeDefaultAddress(Long userId);

    @Update("update address_book set is_default = 1 where id = #{id}")
    void setDefaultAddress(AddressBook addressBook);

    @Select("select * from address_book where id = #{id}")
    AddressBook getAddressBookById(Long id);


    @Update("update address_book set consignee = #{consignee},phone = #{phone},detail = #{detail},label = #{label},sex = #{sex},update_time = #{updateTime} where id = #{id}")
    void updateAddressBook(AddressBook addressBook);

    @Delete("delete from address_book where id = #{ids}")
    void deleteAddressByIds(Long ids);

    @Select("select * from address_book where user_id = #{userId} and is_default = 1")
    AddressBook findDefaultAddressBook(Long userId);

    @Select("select * from address_book where id = #{addressBookId}")
    AddressBook findAddressBookById(Long addressBookId);
}
